#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10
#define SWAP(A, B)                                                             \
  do {                                                                         \
    typeof(A) t = (A);                                                         \
    (A) = (B);                                                                 \
    (B) = (t);                                                                 \
  } while (0)

void quickSort(int left, int right, int nums[]);
void printArr(int n, int nums[]);

int main() {
  srand(time(NULL));
  int nums[N];
  for (int i = 0; i < N; i++) {
    nums[i] = rand() % 100;
  }
  printArr(N, nums);
  quickSort(0, N - 1, nums);
  printArr(N, nums);
  return 0;
}

void printArr(int n, int nums[]) {
  for (int i = 0; i < n; i++)
    printf("%d  ", nums[i]);
  printf("\n");
}

void quickSort(int left, int right, int nums[]) {
  if (left >= right)
    return;
  int i = left, j = right;
  int base = nums[left];
  while (i < j) {
    while (i < j && nums[j] >= base) {
      j--;
    }
    while (i < j && nums[i] <= base) {
      i++;
    }
    SWAP(nums[i], nums[j]);
  }
  SWAP(nums[left], nums[i]);
  quickSort(left, i - 1, nums);
  quickSort(i + 1, right, nums);
}
